//
//  ZJMineApplicationTwoViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/8.
//

#import "ZJMineApplicationTwoViewController.h"
#import "ZJMineApplicationOneTableViewCell.h"
#import "MineNetWorking.h"
#import "MineGoodsTransferModel.h"
#import "HomeTransforShowOrderInfoViewController.h"
#import "publicNodataView.h"


@interface ZJMineApplicationTwoViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger pageNo;
    NSInteger pageSize;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, strong) MineGoodsTransferListNetWorking * manager;
@property (nonatomic, strong) NSMutableDictionary * paramer;
@property (nonatomic,strong) publicNodataView * nodataView;

@end

@implementation ZJMineApplicationTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = COLOR_RGB(244, 244, 244);
    [self configUI];
    [self loadNet];
    
}


-(void)loadNet{
    
    [self.paramer setValue:[NSString stringWithFormat:@"%@",@(pageNo)] forKey:@"pageNo"];
    [self.paramer setValue:[NSString stringWithFormat:@"%@",@(pageSize)] forKey:@"pageSize"];
    [self.manager dispatchTaskWithParam:self.paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] intValue] == 200){
            
        if (self->pageNo == 1) {
            [self.dataSource removeAllObjects];
        }
        NSArray * data = responseObject[@"data"][@"list"];
            
            if (data.count == 0) {
                self.nodataView.hidden = NO;
            }else{
                self.nodataView.hidden = YES;
            }
            
        if (data.count < 20)[self.tableView.mj_footer endRefreshingWithNoMoreData];
        else [self.tableView.mj_footer endRefreshing];
        [self.dataSource  addObjectsFromArray:[MineGoodsTransferModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]]];
           
        [self.tableView reloadData];
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            return;
        }
        [self.tableView.mj_header endRefreshing];

    }];
    
}




- (void)configUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nodataView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [self.nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZJMineApplicationOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZJMineApplicationOneTableViewCell"];
    if (!cell) {
        cell = [[ZJMineApplicationOneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZJMineApplicationOneTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MineGoodsTransferModel * model = self.dataSource[indexPath.row];
    cell.goodsModel = model;
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTransforShowOrderInfoViewController * vc = [HomeTransforShowOrderInfoViewController new];
    MineGoodsTransferModel * model = self.dataSource[indexPath.row];
    vc.ID = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(140);
}


#pragma mark - Lazy Loading
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            pageNo = 1;
            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
            [self loadNet];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            pageNo += 1;
            [self.paramer addEntriesFromDictionary:@{@"pageNo":@(pageNo)}];
            [self loadNet];
        }];
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


-(MineGoodsTransferListNetWorking *)manager{
    if (!_manager){
        _manager = [MineGoodsTransferListNetWorking new];
    }
    return _manager;
}


-(publicNodataView *)nodataView{
    if (!_nodataView) {
        _nodataView = [publicNodataView new];
        _nodataView.hidden = YES;
    }
    return _nodataView;
}

@end
